<?php

namespace App\Http\Controllers\Admin\Wechat;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\WxUser;

class UserController extends Controller
{
    public function index(Request $request){
        if ($request->ajax()) {
            $data = array();
            $data['draw'] = $request->get('draw');
            $start = $request->get('start');
            $length = $request->get('length');
            $order = $request->get('order');
            $columns = $request->get('columns');
            $search = $request->get('search');
            $data['recordsTotal'] = WxUser::count();
            if (strlen($search['value']) > 0) {
                $data['recordsFiltered'] = WxUser::where(function ($query) use ($search) {
                    $query->where('nickname', 'LIKE', '%' . $search['value'] . '%')
                    ->orWhere('phone', '=', $search['value']);
                })->count();
                
                $data['data'] = WxUser::where(function ($query) use ($search) {
                    $query->where('nickname', 'LIKE', '%' . $search['value'] . '%')
                    ->orWhere('phone', '=', $search['value']);
                })
                ->skip($start)->take($length)
                ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                ->get();
                
            }else{
                $data['recordsFiltered'] = WxUser::count();
                $data['data'] = WxUser::
                skip($start)->take($length)
                ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                ->get();
                
            }

            return response()->json($data);
        }
      return view('admin.wechat.user.index');
    }
    
    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
}
